package com.ec.common.utils.ip;

import cn.hutool.core.net.NetUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.ec.common.utils.ServletUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 获取IP方法
 */
public class IpUtils {

    /**
     * 获取客户端IP地址
     * @return 客户端IP地址
     */
    public static String getIpAddr() {
        return ServletUtil.getClientIP(ServletUtils.getRequest());
    }


    /**
     * 判断 ip 是否是内部 ip
     *
     * @param ip ip
     * @return true 是内部，false 不是
     */
    public static boolean internalIp(String ip) {
        return NetUtil.isInnerIP(ip);
    }

    public static String getHostIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException ignored) {
        }
        return "127.0.0.1";
    }

    public static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException ignored) {
        }
        return "未知";
    }
}
